<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('日期')">
              <a-month-picker
                :show-time="false"
                valueFormat="YYYY-MM"
                :placeholder="$t('请选择日期')"
                v-model="queryParam.month"
              ></a-month-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" style="margin-left: 8px" @click="handleExportXls($t('场地收款日报'))" v-has="'SiteCollectionV2ReportController:export'">{{
                $t('导出')
              }}</a-button>
              <a-button type="primary" v-print="'#printme'" style="margin-left: 8px" v-has="'SiteCollectionV2ReportController:print'">{{ $t('打印') }}</a-button>
            </span>
          </a-col>
          <template v-if="toggleSearchStatus"></template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div id="printme">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="name"
        :scroll="{ x: true,y:tableHeight }"
        :columns="columns"
        :dataSource="dataSources"
        :pagination="false"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('详情') }}</a>
        </span>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'SiteCollectionDayReport',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      moment,
      description: '场地收款日报页面',
      // 表头
      columns: [
        {
          title: this.$t('日期'),
          align: 'center',
          dataIndex: 'name',
        },
      ],
      url: {
        list: '/report/day/siteCollectionV2/total',
        exportXlsUrl: '/report/day/siteCollectionV2/totalExportXls',
      },
      isNoPageTotal: true,
      queryParam: {
        month: moment().format('YYYY-MM'),
      },
      dataSources: [],
      isCustom: true,
      isHideData: false,
      thRows: 2,
      taxSetting: localStorage.getItem('taxSetting')
    }
  },
  created() {
    this.taxSetting = localStorage.getItem('taxSetting')
  },
  mounted() {
    let that = this
    document.onkeydown = function (e) {
      let key = window.event.keyCode
      if (key == 75 && event.ctrlKey) {
        window.event.preventDefault()
        that.isHideData = false
        that.queryParam.dataType = 0
        that.searchQuery()
      }
    }
  },
  watch: {
    dataSource(nVal) {
      this.columns = [
        {
          title: this.$t('日期'),
          align: 'center',
          dataIndex: 'name',
          width: 90
        },
      ]
      if (nVal.length > 0 && nVal[0].payTypeList.length > 0) {
        nVal[0].payTypeList.forEach((pt) => {
          let dtl = {
            title: this.$t(pt.payTypeName),
            align: 'center',
            children: [
              {
                title: this.$t('收款笔数'),
                align: 'center',
                dataIndex: 'num' + pt.payType,
                width: 110,
                customRender: (t, r, i) => {
                  if (!t) {
                    t = 0
                  }
                  return t.toFixed(0)
                },
                customCell: (r, i) => {
                  return {
                    style: {
                      textAlign: 'right',
                    },
                  }
                },
              },
              {
                title: this.$t('收款金额'),
                align: 'center',
                dataIndex: 'cash' + pt.payType,
                width: 140,
                customRender: (t, r, i) => {
                  if (!t) {
                    t = 0
                  }
                  return t.toFixed(2)
                },
                customCell: (r, i) => {
                  return {
                    style: {
                      textAlign: 'right',
                    },
                  }
                },
              },
            ],
          }
          if(this.taxSetting != 0){
            dtl = {
              title: this.$t(pt.payTypeName),
              align: 'center',
              children: [
                {
                  title: this.$t('收款笔数'),
                  align: 'center',
                  dataIndex: 'num' + pt.payType,
                  width: 110,
                  customRender: (t, r, i) => {
                    if (!t) {
                      t = 0
                    }
                    return t.toFixed(0)
                  },
                  customCell: (r, i) => {
                    return {
                      style: {
                        textAlign: 'right',
                      },
                    }
                  },
                },
                {
                  title: this.$t('收款金额'),
                  align: 'center',
                  dataIndex: 'cash' + pt.payType,
                  width: 140,
                  customRender: (t, r, i) => {
                    if (!t) {
                      t = 0
                    }
                    return t.toFixed(2)
                  },
                  customCell: (r, i) => {
                    return {
                      style: {
                        textAlign: 'right',
                      },
                    }
                  },
                },
                {
                  title: this.$t('其中税费'),
                  align: 'center',
                  dataIndex: 'tax' + pt.payType,
                  width: 110,
                  customRender: (t, r, i) => {
                    if (!t) {
                      t = 0
                    }
                    return t.toFixed(2)
                  },
                  customCell: (r, i) => {
                    return {
                      style: {
                        textAlign: 'right',
                      },
                    }
                  },
                },
              ],
            }
          }
          this.columns.push(dtl)
        })
      }
      this.dataSources = []
      this.orgData(nVal, 0)
    },
  },
  methods: {
    customRow(r, i){
      return {
        on: {
          click: () => {
            let x = this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i})
            if( x == -1 ){
              this.selectedRowIndex.push('a'+this.ipagination.current+''+i)
            }else{
              this.selectedRowIndex.splice(x, 1)
            }
          }
        },
        style: {
          whiteSpace: 'break-spaces',
          wordBreak: 'break-word',
          overflowWrap: 'break-word',
          backgroundColor: this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i}) > -1 ? '#FFECE6' : '',
          fontWeight: r.name == this.$t('合计') ? 700 : 400,
        }
      }
    },
    orgData(list, i) {
      if (i <= list.length - 1) {
        let data = {}
        data.name = list[i].name
        this.dataSources.push(data)
        this.orgData1(list, 0, i)
      }
    },
    orgData1(list, i, j) {
      if (i <= list[j].payTypeList.length - 1) {
        list[j].payTypeList.forEach((lt) => {
          this.$set(this.dataSources[j], 'num' + lt.payType, lt.num)
          this.$set(this.dataSources[j], 'cash' + lt.payType, lt.pay)
          this.$set(this.dataSources[j], 'tax' + lt.payType, lt.tax)
        })
        i++
        this.orgData1(list, i, j)
      } else if (j < list.length - 1) {
        j++
        this.orgData(list, j)
      }
    },
    initDictConfig() {
      this.customInit()
    },
    customInit() {
      this.isHideData = true
      this.queryParam.dataType = 1
      this.selectedRowIndex = []
      this.loadData(1)
    },
    searchReset() {
      this.queryParam = {
        month: moment().format('YYYY-MM'),
      }
      this.selectedRowIndex = []
      this.customInit()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>